package com.kaizzi.data.jdl.helper;

import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.common.utils.StringUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.CrmMemberService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/15
 */
@Component
public class PageData18 {

    @Resource
    private CrmMemberService crmMemberService;

    public String getData(ExcelModel model){

        List<String> yearWeeks = DateUtil.getYearWeeks();
        List<TempData> tempdatas = new ArrayList<>();

        Page18 page18 = new Page18();

        for (String week : yearWeeks) {
            String[] split = week.split("#");
            if (DateUtil.Compare(split[0] ,split[1], "2020-05-25 00:00:00")){
                continue;
            }
            long registUser = crmMemberService.getRegistUser(split[0], split[1]);
            long oldUser = crmMemberService.getOldUser(split[0], split[1]);
            long activationUser = registUser + oldUser;

            long totalUser = crmMemberService.getUserCount("",split[1],"");

            TempData tempData = new TempData();
            tempData.setRegistUser(registUser);
            tempData.setOldUser(oldUser);
            tempData.setActivationUser(activationUser);
            tempData.setDate(DateUtil.getDateFormat10(split[0],split[1]));

            tempData.setActivationRate(StringUtil.decimalFormat((double)activationUser/(double)totalUser*100));
            tempdatas.add(tempData);
        }

        page18.setData(tempdatas);
        page18.setDataPeriod(DateUtil.getFirstDay2LastSunday());
        page18.setTitle(LogicUtil.getTitle(model,18));

        return JsonUtil.serialize(page18);
    }

    @Data
    private static class TempData{
        private long registUser;
        private long oldUser;
        private long activationUser;
        private String activationRate;
        private String date;
    }


    @Data
    private static class Page18{
//        private String date;
        private List<TempData> data;
        private String dataPeriod;
        private String title;
    }

}
